import 'package:flutter/material.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/common/dao/ReposDao.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/widgets/BaseListState.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';
import 'package:my_flutter_github/widgets/ReposItemWidget.dart';
import 'package:my_flutter_github/widgets/UserItemWidget.dart';

class CommonListPage extends StatefulWidget {

  final String userName;

  final String reposName;

  final String showType;

  final String dataType;

  final String title;

  const CommonListPage({Key key, this.userName, this.reposName, this.showType, this.dataType, this.title}) : super(key: key);

  @override
  _CommonListPageState createState() => _CommonListPageState();
}

class _CommonListPageState extends BaseListState<CommonListPage> {

  _renderItem(index) {
    if (pullLoadWidgetControl.dataList.length == 0) {
      return null;
    }
    var data = pullLoadWidgetControl.dataList[index];
    switch (widget.showType) {
      case 'repository':
        ReposViewModel reposViewModel = ReposViewModel.fromMap(data);
        return new ReposItemWidget(reposViewModel: reposViewModel, onPressed: () {
          NavigatorUtils.goReposDetail(context, reposViewModel.ownerName, reposViewModel.repositoryName);
        });
      case 'user':
        return new UserItemWidget(UserItemViewModel.fromMap(data), onPressed: () {
          NavigatorUtils.goPerson(context, data.login);
        });
      case 'org':
        return new UserItemWidget(UserItemViewModel.fromOrgMap(data), onPressed: () {
          NavigatorUtils.goPerson(context, data.login);
        });
      case 'issue':
        return null;
      case 'release':
        return null;
      case 'notify':
        return null;
    }
  }
  _getDataLogic() async {
    switch (widget.dataType) {
      case 'follower':
        return await UserDao.getFollowerListDao(widget.userName, pageIndex, needDb: pageIndex <= 1);
      case 'followed':
        return await UserDao.getFollowedListDao(widget.userName, pageIndex, needDb: pageIndex <= 1);
      case 'user_repos':
        return await ReposDao.getUserRepositoryDao(widget.userName, pageIndex, null, needDb: pageIndex <= 1);
      case 'user_star':
        return await ReposDao.getStarRepositoryDao(widget.userName, pageIndex, null, needDb: pageIndex <= 1);
      case 'repo_star':
        return await ReposDao.getRepositoryStarDao(widget.userName, widget.reposName, pageIndex, needDb: pageIndex <= 1);
      case 'repo_watcher':
        return await ReposDao.getRepositoryWatcherDao(widget.userName, widget.reposName, pageIndex, needDb: pageIndex <= 1);
      case 'repo_fork':
        return await ReposDao.getRepositoryForksDao(widget.userName, widget.reposName, pageIndex, needDb: pageIndex <= 1);
      case 'repo_release':
        return null;
      case 'repo_tag':
        return null;
      case 'notify':
        return null;
      case 'history':
        return await ReposDao.getHistoryDao(pageIndex);
      case 'topics':
        return await ReposDao.searchTopicRepositoryDao(widget.userName, page: pageIndex);
      case 'user_be_stared':
        return null;
      case 'user_orgs':
        return await UserDao.getUserOrgsDao(widget.userName, pageIndex, needDb: pageIndex <= 1);
    }
  }

  @override
  bool get wantKeepAlive => true;

  @override
  requestRefresh() async {
    return await _getDataLogic();
  }

  @override
  requestLoadMore() async {
    return await _getDataLogic();
  }

  @override
  bool get autoRefresh => true;

  @override
  bool get needHeader => false;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: new Text(
        widget.title,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),

      ),
    body: PullLoadWidget(
      itemBuilder: (context, index) {
          return _renderItem(index);
      },
      control: pullLoadWidgetControl,
      refreshCallback: onRefresh,
      loadMoreCallback: onLoadMore,
      refreshKey: refreshIndicatorKey,
    ),
    );
  }
}
